package com.sso.controller;

import com.sso.service.UserService;
import com.sso.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author : luyifei
 * @date : 2021/1/17
 */
@Controller
@RequestMapping(value = "/api")
public class SsoApiController {

    @Autowired
    UserService userService;

    /**
     * 验证token
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/verifyToken", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Result verifyToken(HttpServletRequest request, HttpServletResponse response) {
        String token = request.getParameter("token");
        if (userService.verifyToken(token)) {
            userService.delayToken(token);
            return Result.buildIsOk("success");
        }
        return Result.buildIsFail("fail");
    }
    /**
     * 退出登录
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/logout", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Result logout(HttpServletRequest request, HttpServletResponse response) {
        String token = request.getParameter("token");
        if (userService.verifyToken(token)) {
            userService.deleteCacheToken(token);
            return Result.buildIsOk("success");
        }
        return Result.buildIsFail("fail");
    }
}
